﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace DataCreateUtility
{
    class Doc_write
    {

        /// <summary>
        /// Method will Generate the Document
        /// </summary>
        /// <param name="File_Name">Name of the File to be saved</param>
        /// <param name="Data">Data which needs to be added in the Document</param>
        public void Doc_generate(String File_Name, String Data)
        {

            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
            object Document_Format;

            //Start Word and create a new document.
            _Application oWord;
            _Document oDoc;
            oWord = new Application();
            oWord.Visible = false;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);

            //Insert a paragraph at the beginning of the document.
            Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = Data;
            oPara1.Range.Font.Bold = 0;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();

            //Set the Format of the Document.

            if (File_Name.ToUpper().Contains(".DOC") && !File_Name.ToUpper().Contains(".DOCX"))
            {
                Document_Format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97;
            }
            else
            {
                Document_Format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
            }


            // Save the created Document.
            Object fileName = File_Name;
            oDoc.SaveAs(ref fileName,
        ref Document_Format, ref oMissing,
        ref oMissing, ref oMissing,
        ref oMissing, ref oMissing,
        ref oMissing, ref oMissing,
        ref oMissing, ref oMissing);
            object notTrue = false;


            //Quit the word process
            oDoc.Application.Quit(ref notTrue,
                     ref oMissing,
                     ref oMissing);
        }

    }
}
